package com.zrrd.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.reggie.bean.*;
import com.zrrd.reggie.common.BaseContext;
import com.zrrd.reggie.common.CustException;
import com.zrrd.reggie.mapper.OrdersMapper;
import com.zrrd.reggie.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    private ShoppingCartService cartService;

    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Override
    public void saveOrder(Orders orders) throws CustException {
        Long userId= BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<ShoppingCart> list = cartService.list(wrapper);
        if (list==null || list.size()==0){
            throw new CustException("购物车为空，不能下单");
        }
        User user=userService.getById(userId);
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        if (addressBook==null){
            throw  new CustException("地址有误，不能下单");
        }
        long orderNumber = IdWorker.getId();
        List<OrderDetail> detailList=new ArrayList<>();
        AtomicInteger amount=new AtomicInteger(0);
        for (ShoppingCart shoppingCart:list){
            OrderDetail detail=new OrderDetail();
            detail.setOrderId(orderNumber);
            detail.setNumber(shoppingCart.getNumber());
            detail.setDishFlavor(shoppingCart.getDishFlavor());
            detail.setDishId(shoppingCart.getDishId());
            detail.setSetmealId(shoppingCart.getSetmealId());
            detail.setName(shoppingCart.getName());
            detail.setImage(shoppingCart.getImage());
            detail.setAmount(shoppingCart.getAmount());
            detailList.add(detail);
            amount.addAndGet(shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber())).intValue());
        }
        orders.setId(orderNumber);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setStatus(1);
        orders.setUserId(userId);
        orders.setNumber(orderNumber+"");
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setAmount(new BigDecimal(amount.get()));

        this.save(orders);
        orderDetailService.saveBatch(detailList);
        cartService.remove(wrapper);
    }

    @Override
    public void again(Orders orders) {
        QueryWrapper<OrderDetail> wrapper=new QueryWrapper<>();
        wrapper.eq("order_id",orders.getId());
        List<OrderDetail> detailList=orderDetailService.list(wrapper);
        List<ShoppingCart> cartList=new ArrayList<>();
        for (OrderDetail detail : detailList) {
            ShoppingCart cart=new ShoppingCart();
            BeanUtils.copyProperties(detail,cart);
            cart.setUserId(BaseContext.getCurrentId());
            cart.setCreateTime(LocalDateTime.now());
            cartList.add(cart);
        }
        cartService.saveBatch(cartList);
    }
}
